
#include <cstdlib>
#include <iostream>

#include "core/client/Sender.h"
#include "core/client/Receiver.h"
#include "network/Socket.h"

int main(int argc,char * argv[])
{
    // Validation des paramètres
    if(argc < 2 || argc > 3)
    {
        std::cerr << "Usage : server [port]" << std::endl;
        return EXIT_FAILURE;
    }

    // Connexion
    Socket* socket = new Socket();
    if(socket->connect(argv[1], argc == 2 ? 31337 : atoi(argv[2])))
    {
        std::cout << "> Connected to "<< argv[1] << std::endl;

        bool* isPilot = new bool(false);
        Sender* sender = new Sender(socket,isPilot);
        Receiver* receiver = new Receiver(socket,isPilot);
        sender->exec();
        receiver->exec();
        sender->join();
        receiver->join();
    }
    // Erreur de connexion
    else
    {
        std::cerr << "> " << socket->errorString() << std::endl;
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}
